package com.eveinfo.display.asset;

import java.util.ArrayList;
import java.util.Comparator;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;

import com.eveinfo.R;
import com.eveinfo.display.SheetView;
import com.eveinfo.sql.data.Item;
import com.eveinfo.tools.Logging;

public class AssetsByItemsView extends Activity {

	private static final int MENU_SORT = 0;
	private static final String MENU_SORT_NAME = "Sort";
	private static final int SEARCH_TIME_DELAY = 600;

	private AssetsByItemsAdapter assetsItemsAdapter;

	private EditText editText;
	private Activity activity;
	private SearchCountDown searchThread;

	public AssetsByItemsView() {
		this.searchThread = new SearchCountDown(SEARCH_TIME_DELAY, SEARCH_TIME_DELAY);
		this.activity = this;
	}

	private enum Items {
		group("group", new GroupComparator<Item>()), nameC("name", new NameComparator<Item>());

		private final String name;
		private final Comparator<Item> sorter;

		private Items(String name, Comparator<Item> sorter) {
			this.name = name;
			this.sorter = sorter;
		}

		public Comparator<Item> getSorter() {
			return sorter;
		}

		@Override
		public String toString() {
			return name;
		}
	}

	class SearchCountDown extends CountDownTimer {

		private String search;

		public SearchCountDown(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			assetsItemsAdapter.setSeach(search);
		}

		@Override
		public void onTick(long millisUntilFinished) {
		}

		public void setSearch(String search) {
			this.search = search;
		}

		public String getSearch() {
			return search;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.assets);

		final ListView listView = (ListView) findViewById(R.id.LVAssets);
		try {
			ArrayList<Item> items = SheetView.getItems();
			assetsItemsAdapter = new AssetsByItemsAdapter(activity, listView, items);
			assetsItemsAdapter.setSeach("");
			editText = (EditText) findViewById(R.id.ETAssets);
			editText.setOnKeyListener(new OnKeyListener() {
				@Override
				public boolean onKey(View v, int keyCode, KeyEvent event) {
					if (keyCode == 82 || keyCode == 4) {
						return false;
					}
					if (event.getAction() == KeyEvent.ACTION_DOWN)
						return false;
					searchThread.setSearch(editText.getText().toString());
					searchThread.cancel();
					searchThread.start();
					return false;
				}
			});

		} catch (Exception e) {
			e.printStackTrace();
			Logging.getInstance().e(e);
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(e.toString());
			builder.setMessage(e.getMessage());
			builder.show();
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_SORT:
			showDialog(MENU_SORT);
			return true;
		default:
			return false;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_SORT, 0, MENU_SORT_NAME);
		return true;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog;
		switch (id) {
		case MENU_SORT:
			dialog = new Dialog(this);
			dialog.setTitle("Sort by :");

			LinearLayout layout = new LinearLayout(this);
			layout.setOrientation(LinearLayout.VERTICAL);

			final Spinner spinner = new Spinner(this);
			ArrayAdapter<Items> adapter = new ArrayAdapter<Items>(this, android.R.layout.simple_spinner_item, Items
					.values());
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner.setAdapter(adapter);
			layout.addView(spinner);

			Button button = new Button(this);
			button.setText("Sort");
			button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Items i = (Items) spinner.getSelectedItem();
					assetsItemsAdapter.setSort(i.getSorter());
					assetsItemsAdapter.setSeach(editText.getText().toString());
					dismissDialog(MENU_SORT);
				}
			});
			layout.addView(button);

			dialog.setContentView(layout);
			break;
		default:
			dialog = null;
			break;
		}
		return dialog;
	}
}
